/**
 * Created by John on 2016/7/11.
 */
var mongo = new Mongo("localhost");
var wordsDB = mongo.getDB("BeginningMongoDB");
wordsColl = wordsDB.getCollection("word_stats");

wordsDB.runCommand({getLastError: 1, w: 1, j: true, wtimeout: 1000});

var and = {word: 'and'};
var projection = {word: 1, size: 1, stats: 1, letters: 1};
var cursor = wordsColl.find(and, projection);
print('Before Upsert:');
printjson(cursor.toArray());

wordsColl.update(and, {
    $set: {
        word: 'and',
        size: 6,
        letters: ['a', 'n', 'd'],
        'stats.consonants': 5,
        'stats.vowels:': 4
    }
}, true, true);
cursor = wordsColl.find(and, projection);
print('After Upsert:');
printjson(cursor.toArray());

wordsColl.update(and, {
    $set: {
        word: 'and',
        size: 3,
        letters: ['a', 'n', 'd'],
        'stats.consonants': 2,
        'stats.vowels:': 1
    }
}, true, true);
cursor = wordsColl.find(and, projection);
print('After Second Upsert:');
printjson(cursor.toArray());